package com.lsy.weizhi2.activity;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lsy.weizhi2.R;
import com.lsy.weizhi2.config.MyHeight;
import com.lsy.weizhi2.fragment.IAskOtherFragment;
import com.lsy.weizhi2.fragment.MyActivityFragment;
import com.lsy.weizhi2.fragment.MyWeiZhiFragment;
import com.lsy.weizhi2.fragment.OtherAskMeFragment;
import com.lsy.weizhi2.mybase.MyBaseActivity;
import com.lsy.weizhi2.mybase.MyTitleView;

import java.util.ArrayList;
import java.util.List;

public class ConcludeManager extends MyBaseActivity implements View.OnClickListener,ViewPager.OnPageChangeListener{
    private Context context;
    private MyTitleView manager_title;
    private TextView manager_send_tv,manager_receive_tv;
    private View manager_send,manager_receive;
    private ViewPager manager_vp;
    private IAskOtherFragment iAskOtherFragment;
    private OtherAskMeFragment otherAskMeFragment;
    private List<Fragment> list_f;
    private FragmentManager fragmentManager;
    private PageAdapter adapter;
    private int xx,index;
    private Handler handler=new Handler();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_conclude_manager);
        context=ConcludeManager.this;
        Init();
    }

    private void Init() {
        manager_title= (MyTitleView) findViewById(R.id.manager_title);
        manager_send_tv= (TextView) findViewById(R.id.manager_send_tv);
        manager_receive_tv= (TextView) findViewById(R.id.manager_receive_tv);
        manager_send=findViewById(R.id.manager_send);
        manager_receive=findViewById(R.id.manager_receive);
        manager_vp= (ViewPager) findViewById(R.id.manager_vp);
        iAskOtherFragment=new IAskOtherFragment();
        otherAskMeFragment=new OtherAskMeFragment();
        list_f=new ArrayList<>();
        list_f.add(iAskOtherFragment);
        list_f.add(otherAskMeFragment);
        fragmentManager=getSupportFragmentManager();
        adapter=new PageAdapter(fragmentManager);
        manager_vp.setAdapter(adapter);
        manager_vp.setCurrentItem(index);
        manager_send_tv.setOnClickListener(this);
        manager_receive_tv.setOnClickListener(this);
        manager_vp.addOnPageChangeListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i("ConcludeManager","onResume");
        ShowItem();
        xx=0;
        new Thread(new Runnable() {
            @Override
            public void run() {
                ViewTreeObserver vto = manager_title.getViewTreeObserver();
                vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        if (xx < 1) {
                            xx++;
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(manager_title.getLayoutParams());
                                    params.height = MyHeight.ACTIONBAR + MyHeight.MyTitleHeight;
                                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                                        manager_title.setLayoutParams(params);
                                    }
                                }
                            });
                        }
                    }
                });
            }
        }).start();
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i("ConcludeManager","onPause");
        xx=0;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.manager_receive_tv:
                index=1;
                manager_vp.setCurrentItem(index);
                ShowItem();
                break;
            case R.id.manager_send_tv:
                index=0;
                manager_vp.setCurrentItem(index);
                ShowItem();
                break;
        }
    }

    private void ShowItem(){
        if(index==0){
            manager_send_tv.setTextColor(getResources().getColor(R.color.clicked_ok));
            manager_receive_tv.setTextColor(getResources().getColor(R.color.classify_text));
            manager_send.setVisibility(View.VISIBLE);
            manager_receive.setVisibility(View.INVISIBLE);
        }else if(index==1){
            manager_receive_tv.setTextColor(getResources().getColor(R.color.clicked_ok));
            manager_send_tv.setTextColor(getResources().getColor(R.color.classify_text));
            manager_send.setVisibility(View.INVISIBLE);
            manager_receive.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        index=position;
        ShowItem();
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    class PageAdapter extends FragmentPagerAdapter {

        public PageAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {

            return list_f.get(position);
        }

        @Override
        public int getCount() {
            return list_f.size();
        }
    }
}
